package com.collection.provider.service.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.collection.api.entity.sys.SysResource;
import com.collection.api.entity.sys.SysRole;
import com.collection.api.entity.sys.SysRoleResource;
import com.collection.api.entity.sys.SysUser;
import com.collection.api.sys.SysRoleService;
import com.collection.provider.biz.SysResourceBiz;
import com.collection.provider.biz.SysRoleBiz;

import freemarker.template.utility.StringUtil;

/**
 * @descrption:角色管理Biz
 * @author :HanDiYan
 * @Date : 2017年12月27日
 **/
@Service(version ="1.0.0")
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleBiz sysRoleBiz;
	@Autowired
	private SysResourceBiz sysResourceBiz;


	@Override
	public List<Map<String, Object>> findRoleList(String roleName, String userType, Integer offset, Integer limit) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(roleName)){
			map.put("roleName", roleName);
		}
		if(null != offset && !"".equals(offset)){
			map.put("offset", offset);
		}
		if(null != limit && !"".equals(limit)){
			map.put("limit", limit);
		}
		return sysRoleBiz.findRoleList(map);
	}

	@Override
	public int countRoleList(String roleName, String userType) {
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isNotBlank(roleName)){
			map.put("roleName", roleName);
		}
		return sysRoleBiz.countRoleList(map);
	}

	@Override
	public List<SysResource> findAllResource() {
        List<SysResource> list = sysResourceBiz.findAllResource();
        return list;
	}

	@Override
	public List<Map<String, Object>> findResourcesByRoleId(int roleId, Integer appId) {
		return sysRoleBiz.findResourcesByRoleId(roleId, appId);
	}

	@Override
	public void insertSelective(String name, String userType, Integer appId, List<String> resourceIds) {
		sysRoleBiz.saveRole(name, userType, appId, resourceIds);
	}

	@Override
	public void updateByPrimaryKeySelective(Integer roleId, String name, String userType, Integer appId,
			List<String> resourceIds) {
		sysRoleBiz.updateRole(roleId, name, userType, appId, resourceIds);
	}

	@Override
	public SysRole selectByPrimaryKey(Integer roleId) {
		return sysRoleBiz.selectByPrimaryKey(roleId);
	}

	@Override
	public int countUserByRoleId(int roleId) {
		return sysRoleBiz.countUserByRoleId(roleId);
	}

	@Override
	public void delRoleByRoleId(int roleId) {
		sysRoleBiz.deleteRoleAndRoleResourceByRoleId(roleId);
	}

	@Override
	public List<SysRole> findRoleListAll() {
		return sysRoleBiz.findRoleListAll();
	}

}
